In [1]:
import plotly.offline as pyo

from plotly.graph_objs import *

import chart_studio.plotly as py

import pandas as pd
from pandas import DataFrame
In [2]:
pyo.offline.init_notebook_mode()
In [3]:
emissions = pd.read_csv(r"../Data/TotalCo2EmissionsByCountry.csv", index_col=0)
emissions.head()
Out[3]:
Year Afghanistan | AFG Albania | ALB Algeria | DZA American Samoa | ASM Andorra | AND Angola | AGO Antigua and Barbuda | ATG Arab World | ARB Argentina | ARG ... Uzbekistan | UZB Vanuatu | VUT Venezuela, RB | VEN Vietnam | VNM Virgin Islands (U.S.) | VIR West Bank and Gaza | PSE World | WLD Yemen, Rep. | YEM Zambia | ZMB Zimbabwe | ZWE
0 1960 414.371 2024.184 6160.560 NaN NaN 550.050 36.670 59563.98922 48815.104 ... NaN NaN 57069.521 7491.681 NaN NaN 9.396706e+06 3633.997 NaN NaN
1 1961 491.378 2280.874 6065.218 NaN NaN 454.708 47.671 65151.09581 51180.319 ... NaN NaN 51928.387 7986.726 NaN NaN 9.434403e+06 2665.909 NaN NaN
2 1962 689.396 2464.224 5669.182 NaN NaN 1180.774 102.676 74357.70773 53695.881 ... NaN 40.337 54106.585 9347.183 NaN NaN 9.818840e+06 3887.020 NaN NaN
3 1963 707.731 2082.856 5427.160 NaN NaN 1151.438 84.341 87895.97916 50083.886 ... NaN 33.003 56204.109 9119.829 NaN NaN 1.035575e+07 2918.932 NaN NaN
4 1964 839.743 2016.850 5650.847 NaN NaN 1224.778 91.675 103196.28160 55727.399 ... NaN 62.339 56603.812 11800.406 NaN NaN 1.094701e+07 3633.997 3278.298 4473.74

5 rows × 249 columns

In [4]:
def createStackedQuantArea(df, time, cols, hover, title, yaxisTitle): 
    """
    A function which manipulates the data into the correct format to produce a stacked quantity area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text common to every hoverlabel
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
        
    return stackedAreaDF
   
test = createStackedQuantArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND'], 'Total C02 Emissions: ',
                            "Quantity of Co2 Emissions, 1960-2011", 'Quantity of Co2 Emissions')
test.head()
Out[4]:
Year United Arab Emirates | ARE United Kingdom | GBR United States | USA China | CHN India | IND
0 1960 11.001 584299.780 2890696.100 780726.302 120581.961
1 1961 11.001 588938.535 2880505.507 552066.850 130402.187
2 1962 18.335 593360.937 2987207.873 440359.029 143467.708
3 1963 22.002 603822.888 3119230.874 436695.696 154083.673
4 1964 18.335 608355.300 3255995.306 436923.050 150647.694
In [5]:
def createStackedQuantArea(df, time, cols, hover, title, yaxisTitle):
    """
    A function which manipulates the data into the correct format to produce a stacked quantity area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text common to every hoverlabel
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
    
    cumulative = stackedAreaDF[cols].cumsum(axis = 1)
        
    return cumulative
   
test = createStackedQuantArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND'],  'Total C02 Emissions: ',
                            "Quantity of Co2 Emissions, 1960-2011", 'Quantity of Co2 Emissions')
test.head()
Out[5]:
United Arab Emirates | ARE United Kingdom | GBR United States | USA China | CHN India | IND
0 11.001 584310.781 3475006.881 4255733.183 4376315.144
1 11.001 588949.536 3469455.043 4021521.893 4151924.080
2 18.335 593379.272 3580587.145 4020946.174 4164413.882
3 22.002 603844.890 3723075.764 4159771.460 4313855.133
4 18.335 608373.635 3864368.941 4301291.991 4451939.685
In [6]:
def createStackedQuantArea(df, time, cols, hover, title, yaxisTitle):
    """
    A function which manipulates the data into the correct format to produce a stacked quantity area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text common to every hoverlabel
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
    
    cumulative = stackedAreaDF[cols].cumsum(axis = 1)
    
    cumulativeAndOrig = cumulative.merge(stackedAreaDF, 
                                         left_index = True,
                                         right_index = True,
                                        suffixes = ('_c','_o'))
        
    return cumulativeAndOrig
   
test = createStackedQuantArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND'],  'Total C02 Emissions: ',
                            "Quantity of Co2 Emissions, 1960-2011", 'Quantity of Co2 Emissions')
test.head()
Out[6]:
United Arab Emirates | ARE_c United Kingdom | GBR_c United States | USA_c China | CHN_c India | IND_c Year United Arab Emirates | ARE_o United Kingdom | GBR_o United States | USA_o China | CHN_o India | IND_o
0 11.001 584310.781 3475006.881 4255733.183 4376315.144 1960 11.001 584299.780 2890696.100 780726.302 120581.961
1 11.001 588949.536 3469455.043 4021521.893 4151924.080 1961 11.001 588938.535 2880505.507 552066.850 130402.187
2 18.335 593379.272 3580587.145 4020946.174 4164413.882 1962 18.335 593360.937 2987207.873 440359.029 143467.708
3 22.002 603844.890 3723075.764 4159771.460 4313855.133 1963 22.002 603822.888 3119230.874 436695.696 154083.673
4 18.335 608373.635 3864368.941 4301291.991 4451939.685 1964 18.335 608355.300 3255995.306 436923.050 150647.694
In [7]:
def createStackedQuantArea(df, time, cols, hover, title, yaxisTitle):
    """
    A function which manipulates the data into the correct format to produce a stacked quantity area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    hover - the text common to every hoverlabel
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
    
    cumulative = stackedAreaDF[cols].cumsum(axis = 1)
    
    cumulAndOrig = cumulative.merge(stackedAreaDF, 
                                         left_index = True,
                                         right_index = True,
                                        suffixes = ('_c','_o'))
    
    for col in cols:
        cumulAndOrig[col + '_t'] = "<b>" + str(col)[:-6] + "</b><br>" + str(hover) + cumulAndOrig[col + "_o"].apply(lambda x:
            "{:,}Kt".format(int(round(x))))
        
    return cumulAndOrig
   
test = createStackedQuantArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND'], 'Total C02 Emissions: ',
                            "Quantity of Co2 Emissions, 1960-2011", 'Quantity of Co2 Emissions')
test.head(1)
Out[7]:
United Arab Emirates | ARE_c United Kingdom | GBR_c United States | USA_c China | CHN_c India | IND_c Year United Arab Emirates | ARE_o United Kingdom | GBR_o United States | USA_o China | CHN_o India | IND_o United Arab Emirates | ARE_t United Kingdom | GBR_t United States | USA_t China | CHN_t India | IND_t
0 11.001 584310.781 3475006.881 4255733.183 4376315.144 1960 11.001 584299.78 2890696.1 780726.302 120581.961 <b>United Arab Emirates</b><br>Total C02 Emiss... <b>United Kingdom</b><br>Total C02 Emissions: ... <b>United States</b><br>Total C02 Emissions: 2... <b>China</b><br>Total C02 Emissions: 780,726Kt <b>India</b><br>Total C02 Emissions: 120,582Kt
In [10]:
def createStackedQuantArea(df, time, cols, hover, title, yaxisTitle):
    """
    A function which manipulates the data into the correct format to produce a stacked quantity area plot with Plotly.
    
    Takes five arguments:
    
    df - a pandas DataFrame
    time - the time element of the data, must be a column in the DataFrame
    cols - the name of the columns in the DataFrame which you want to include in the area plot
    title - the title of the chart
    yaxisTitle - the yaxis title of the chart (the xaxis title comes from the time variable)
    """
    traces = []
    stackedAreaDF = df.loc[:, ([time] + cols)]
    stackedAreaDF.fillna(0, inplace=True)
    
    cumulative = stackedAreaDF[cols].cumsum(axis = 1)
    
    cumulAndOrig = cumulative.merge(stackedAreaDF, 
                                         left_index = True,
                                         right_index = True,
                                        suffixes = ('_c','_o'))
    
    for col in cols:
        cumulAndOrig[col + '_t'] = "<b>" + str(col)[:-6]  + "</b><br>" + str(hover) + cumulAndOrig[col + "_o"].apply(lambda x:
            "{:,}Kt".format(int(round(x, 0))))
        
        traces.append({'type' : 'scatter',
                      'x' : cumulAndOrig[time],
                      'y' : cumulAndOrig[col + "_c"],
                       'text' : cumulAndOrig[col + "_t"],
                       'hoverinfo' : 'text+x',
                      'name' : col[:-6],
                      'mode' : 'lines',
                      'fill' : 'tonexty'})
        
    data = Data(traces)
    layout = {'title' : title,
             'xaxis' : {'title' : time},
             'yaxis' : {'title' : yaxisTitle,
                       'ticksuffix' : ' Kt'},
             'hovermode' : 'closest'}
    fig = Figure(data = data, layout = layout)
    pyo.iplot(fig)
    return fig
    
    
C02Quant = createStackedQuantArea(emissions, 'Year', ['United Arab Emirates | ARE','United Kingdom | GBR', 
                   'United States | USA','China | CHN', 'India | IND'], 'Total C02 Emissions: ',
                            "Quantity of Co2 Emissions, 1960-2011", 'Quantity of Co2 Emissions')
In [ ]: